package com.gmu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblStudentMapper;
import com.gmu.pojo.TblStudent;
import com.gmu.pojo.TblTeacher;
import com.gmu.pojo.TblUser;
import com.gmu.service.TblStudentService;
import com.gmu.service.TblUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@RestController
@Api(tags = "学生信息表")
@RequestMapping("/gmu/tbl-student")
public class TblStudentController {

    @Resource
    private TblStudentService studentService;

    @Resource
    private TblUserService userService;

    @Autowired
    private TblStudentMapper tblStudentMapper;

    //查询所有学生信息
    @GetMapping("listAll")
    @ApiOperation("所有学生信息")
    public ResResult listAll(){
        List<TblStudent> studentList = studentService.list(null);
        return new ResResult(200,"查询所有学生信息成功",studentList);
    }

    //添加或修改学生的方法
    @PostMapping("addStudent")
    @ApiOperation("添加和修改学生信息")
    public ResResult addStudent(@RequestBody TblStudent tblStudent){
        QueryWrapper<TblUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id_number", tblStudent.getIdNumber());
        TblUser tblUser = userService.getOne(wrapper);
        if(tblUser==null){
            return new ResResult(500,"该身份证暂未注册，请先注册");
        }
        Integer userId = tblUser.getUserId();
        tblStudent.setUserId(userId);
        boolean save = studentService.saveOrUpdate(tblStudent);
        if(save){
            return new ResResult(200,"执行成功");
        }else{
            return new ResResult(500,"执行失败");
        }
    }

    //根据id查询学生信息
    //用于修改前的数据回显
    @GetMapping("byIdStudent/{studentId}")
    @ApiOperation("根据id查询学生信息")
    public ResResult byIdStudent(@PathVariable Integer studentId){
        TblStudent studentById = studentService.getById(studentId);
        return new ResResult(200,"查询成功",studentById);
    }

    //根据id删除学生信息
    @DeleteMapping("deleteStudent/{studentId}")
    @ApiOperation("删除学生信息")
    public ResResult deleteStudent(@PathVariable Integer studentId){
        Integer userId = studentService.getById(studentId).getUserId();
        boolean removeById = studentService.removeById(studentId);
        userService.removeById(userId);
        if(removeById){
            return new ResResult(200,"删除成功");
        }else{
            return new ResResult(500,"删除失败");
        }
    }

    // 分页查询学生的方法
    @ApiOperation("分页查询")
    @GetMapping("pageStudent/{current}/{limit}")
    public ResResult pageListStudent(@PathVariable long current,
                                     @PathVariable long limit) {
        //创建page对象
        Page<TblStudent> pageStudent = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageTeacher对象里面
        studentService.page(pageStudent, null);

        long total = pageStudent.getTotal();//总记录数
        List<TblStudent> records = pageStudent.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return new ResResult(200,map);
    }

    @ApiOperation(value = "搜索")
    @PostMapping("/getStudentByCondition")
    public ResResult<?>getTblStudentByCondition(@RequestBody TblStudent tblStudent){
        return studentService.getTblStudentByCondition(tblStudent);
    }

//    @ApiOperation(value = "批量删除学生信息")
//    @DeleteMapping("/deleteUserByIds/{userIds}")
//    public ResResult<?> deleteUserByIds(@PathVariable Integer[] userIds){
//        Integer deleteTotal = tblStudentMapper.deleteUserByIds(userIds);
//        if (deleteTotal < 1){
//            return new ResResult<>(500,"删除失败");
//        }
//        return new ResResult<>(200,"本次删除" + deleteTotal + "条记录");
//    }

    @ApiOperation(value = "批量删除学生信息")
    @DeleteMapping("/deleteUserByIds/{userIds}")
    public ResResult<?> deleteUserByIds(@PathVariable Integer[] userIds){
        Integer deleteTotal = tblStudentMapper.deleteUserByIds(userIds);
        if (deleteTotal < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"本次删除" + deleteTotal + "条记录");
    }

}

